package micemap.cn.edu.lzu;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
import javax.servlet.http.HttpServletResponse;


public class PicsTransfer implements Runnable{

/**
 * @param args
 */
public String sourceURL=null;
public String destinationURL=null;
public String fileName=null;
public Long   timeSet=0l;
public int    flag=1;
public HttpServletResponse response=null;
public String configPath;

public PicsTransfer(HttpServletResponse response,String configPath)
{
	
	this.response=response;
	this.configPath=configPath;	
	config();
	 
}

//load the configuration file 
public void config()
{
	try{
			
		String config_path = configPath +  "/config/picdown.properties" ;
		FileInputStream in = new FileInputStream(config_path);
		Properties p=new Properties();
		p.load(in);
		sourceURL=p.getProperty("sourceURL");
		destinationURL=p.getProperty("destinationURL");
		fileName=p.getProperty("fileName");
		timeSet=Long.parseLong(p.getProperty("timeSet"));
		flag=Integer.parseInt(p.getProperty("flag"));
	
	}
	catch(FileNotFoundException  ex)
	{
		System.out.println("The config file is Not Found!");
	}
	catch(IOException e)
	{
		System.out.println("Loading file is failure!");
	}
}
//download localfile
public boolean localTransfer()
{
try {  
           FileInputStream in = new java.io.FileInputStream(sourceURL+fileName);  
               FileOutputStream out = new FileOutputStream(destinationURL+fileName);  
              byte[] bt = new byte[1024*100];  
             int count;  
             while ((count = in.read(bt)) > 0)
             {  
                     out.write(bt, 0, count);  
             }  
              System.out.println("transtion is over!");
             in.close();  
             out.close();  
             return true;  
      }
catch (IOException ex)
   {  
	System.out.println("loacal file transfer is Wrong:"+ex);
             	return false;  
       }  
}

// download the NetFile
 public void downloadNet(){
      
        int bytesum = 0;
        int byteread = 0;

        
        try {
        	URL url = new URL(sourceURL);
            URLConnection conn = url.openConnection();
            InputStream inStream = conn.getInputStream();
            FileOutputStream fs = new FileOutputStream(configPath+destinationURL+fileName);

            byte[] buffer = new byte[1204*100];
            int length;
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                System.out.println(bytesum);
                fs.write(buffer, 0, byteread);
            }
            fs.flush();
            fs.close();
            inStream.close();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (MalformedURLException e2) {
        	e2.printStackTrace();
        }catch (IOException e3) {
            e3.printStackTrace();
        }
    }


public void run() {
// TODO Auto-generated method stub
	while(true)
		{ 
			if(flag==0)
			{
				localTransfer();
			}
			else
			{
				downloadNet();
			}
			try
			{
				System.out.println("The update is finished,go to sleep!");
				Thread.sleep(timeSet*60*1000);
				System.out.println("Start to update!");
			}	
			catch(Exception ex)
			{
				System.out.println("There is something wrong with the thread:"+ex);
			}
		}
	}

}